# add the extra targets in the case we want on-the-fly grammar compiler
find_package ( BISON QUIET )
set ( BISON_DIR "${MANTICORE_BINARY_DIR}/config" )
set ( BIS_FLAGS "" )
if (BISON_VERSION VERSION_GREATER 3.0)
	set ( BIS_FLAGS "-Wno-deprecated" )
endif ()

function ( MY_BISON_ALLOWING_UNITY ParserName ParserSrc Dependency TargetBison )
	if (NOT BISON_FOUND)
		if (EXISTS "${BISON_DIR}/bis${ParserSrc}.c" AND EXISTS "${BISON_DIR}/bis${ParserSrc}.h")
			infomsg ( "Will use pre-built ${BISON_DIR}/bis${ParserSrc}.c and ${BISON_DIR}/bis${ParserSrc}.h for grammar parser ${ParserName}" )
			return ()
		else ()
			message ( FATAL_ERROR "No pre-compiled grammar files bis${ParserSrc}.c and bis${ParserSrc}.h for ${ParserSrc}.y exists, and Bison not found. Can't continue" )
		endif ()
	endif ()

	LIST ( APPEND ${TargetBison}_BISON "${ParserSrc}.y" )
	set ( ${TargetBison}_BISON ${${TargetBison}_BISON} PARENT_SCOPE )
	BISON_TARGET ( ${ParserName} "${CMAKE_CURRENT_SOURCE_DIR}/${ParserSrc}.y" "${BISON_DIR}/bis${ParserSrc}.c" COMPILE_FLAGS ${BIS_FLAGS} )
	set_source_files_properties ( ${Dependency} PROPERTIES OBJECT_DEPENDS ${BISON_${ParserName}_OUTPUT_SOURCE} )
endfunction ()

function ( MY_BISON ParserName ParserSrc Dependency TargetBison )
	if (NOT BISON_FOUND)
		if (EXISTS "${BISON_DIR}/bis${ParserSrc}.c" AND EXISTS "${BISON_DIR}/bis${ParserSrc}.h")
			infomsg ( "Will use pre-built ${BISON_DIR}/bis${ParserSrc}.c and ${BISON_DIR}/bis${ParserSrc}.h for grammar parser ${ParserName}" )
			return ()
		else ()
			message ( FATAL_ERROR "No pre-compiled grammar files bis${ParserSrc}.c and bis${ParserSrc}.h for ${ParserSrc}.y exists, and Bison not found. Can't continue" )
		endif ()
	endif ()

	LIST ( APPEND ${TargetBison}_BISON "${ParserSrc}.y" )
	set ( ${TargetBison}_BISON ${${TargetBison}_BISON} PARENT_SCOPE )
	BISON_TARGET ( ${ParserName} "${CMAKE_CURRENT_SOURCE_DIR}/${ParserSrc}.y" "${BISON_DIR}/bis${ParserSrc}.c" COMPILE_FLAGS ${BIS_FLAGS} )
	set_source_files_properties ( ${Dependency} PROPERTIES OBJECT_DEPENDS ${BISON_${ParserName}_OUTPUT_SOURCE} SKIP_UNITY_BUILD_INCLUSION ON )
endfunction ()